package com.lm.ledger.action;

import java.util.Map;

import com.lm.ledger.dao.LedgerDao;
import com.lm.ledger.dao.LedgerDaoImp;
import com.lm.ledger.service.TransactionService;
import com.lm.ledger.service.TransactionServiceImp;
import com.lm.ledger.utill.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{



    public String execute() throws Exception {
        System.out.println("Validating login");
        System.out.println("username = "+getUsername());
        System.out.println("pwd - "+getPassword());
        
    	TransactionService service = new TransactionServiceImp();
        ActionContext  context = ActionContext.getContext();
		Map session = context.getSession();
    	User user = service.userDetail(getUsername());
    	if(user!=null){
    		String name = user.getName();
        	String pwd = user.getPwd();
        	String insanId = user.getInsanNo();
        	String role = user.getRoleName();  
        	System.out.println("name ***=== "+name);
    		System.out.println("role ===*** "+role);
    		if(role!=null){
    			System.out.println("role is not null");
    			session.put("USERROLE", role);
    		}else{
    			System.out.println("role is null");
    		}
    		
        	if(!getUsername().equalsIgnoreCase(insanId) || !getPassword().equalsIgnoreCase(pwd)){
                addActionError("Invalid user name or password! Please try again!");
                return ERROR;
    		}else{
    			
    			session.put("USER", user);
    			if("operator".equalsIgnoreCase(role) || "Authorizer".equalsIgnoreCase(role)){
    				
    				return SUCCESS;
    			}else{
    				addActionError("Role is nor correct!");
    	            return ERROR;
    			}
    			
    		}
    	}else{
    		return ERROR;
    	}
    	
    	
        
		/*if(!getUsername().equals("Admin") || !getPassword().equals("Admin")){
            addActionError("Invalid user name or password! Please try again!");
            return ERROR;
		}else{
			return SUCCESS;
		}*/
	}

    public void testUser(){
    	
    	LedgerDao dao = new LedgerDaoImp();
    	User user = dao.userDetail(getUsername());
    	String name = user.getName();
    	String pwd = user.getPwd();
    	String role = user.getRoleName();
    	
    }

    // ---- Username property ----

    /**
     * <p>Field to store User username.</p>
     * <p/>
     */
    private String username = null;


    /**
     * <p>Provide User username.</p>
     *
     * @return Returns the User username.
     */
    public String getUsername() {
        return username;
    }

    /**
     * <p>Store new User username</p>
     *
     * @param value The username to set.
     */
    public void setUsername(String value) {
        username = value;
    }

    // ---- Username property ----

    /**
     * <p>Field to store User password.</p>
     * <p/>
     */
    private String password = null;


    /**
     * <p>Provide User password.</p>
     *
     * @return Returns the User password.
     */
    public String getPassword() {
        return password;
    }

    /**
     * <p>Store new User password</p>
     *
     * @param value The password to set.
     */
    public void setPassword(String value) {
        password = value;
    }
}
